package testmybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import testmybatis.springmybatis.MyMapperScan;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Description TODO
 * @Author dangxianyue
 * @Date 2024/3/27 15:29
 */
// ComponentScan比较特殊：可以有多个
@ComponentScan("testmybatis.service")
// @ComponentScan("testmybatis.springmybatis")
// @Import(MyMybatisImportBeanDefinitionRegistrar.class) // 移到@MyMapperScan注解中
@MyMapperScan("testmybatis.mapper")
public class TestMybatisAppConfig {

    /**
     * 加载SqlSessionFactory
     * @return
     * @throws IOException
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws IOException {
        final InputStream is = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        return new SqlSessionFactoryBuilder().build(is);
    }
}
